<template>
  <view class="sszbo-container">
    <image class="kv-img" src="https://scoringchina.oss-cn-beijing.aliyuncs.com/biemlfGolf/kkk.jpg" mode="widthFix" />
    <view class="btn-list" v-for="(item, index) in groupList" :key="index">
      <view class="btn-item btn-red" @click="onBtnClick1(item)">
        <image class="btn-icon" :src="item.thumb" mode="aspectFit" />
      </view>
    </view>
      <view class="btn-list">
      <view class="btn-item btn-purple" @click="onBtnClick2('wonderful')">
        <image class="btn-icon" src="https://scoringchina.oss-cn-beijing.aliyuncs.com/biemlfGolf/icon_jczb%402x.png" mode="aspectFit" /> 
     </view> 
    </view>
  </view>
</template>
<script>
export default {
  name: "SszboPage",
  data() {
    return {
      richTextContent: "",
      groupType: '',
      groupList: []
    }
  },
  onLoad(e) {
    console.log('-----------1------22-----', e.group)
    this.groupType = e.group
    this.fetchNews();
		
  },
  mounted() {
  this.initWechatSDK()
  },
  methods: {
	async initWechatSDK() {
	  try {
	    await this.loadWxSdk();  // 加载微信 SDK
	
	  const urlForSign = window.location.origin + '/index.html';

	   console.log('urlForSign--------',urlForSign)
	const res = await fetch(
	  'http://chaoxiang.scoringchina.com/index/api/getwxjsapi?url=' + encodeURIComponent(urlForSign)
	);

	    const config = await res.json();
	    console.log('微信 config 数据:', config);
		let appid  = config.data.app_id;
		let timestamp =  config.data.timestamp;
		let nonceStr = config.data.noncestr;
		let signature = config.data.signature;
		console.log('---2---',nonceStr)
		console.log('---1-timestamp--',timestamp)
		console.log(appid,'1--2---')
		console.log('---1-----signature----',signature)
	
	    wx.config({
	      debug: false,
	      appId: appid,
	      timestamp: timestamp,
	      nonceStr:nonceStr,
	      signature:signature,
	      jsApiList: ['checkJsApi']
	    });
	
	    wx.ready(() => {
	      console.log('微信 SDK 初始化成功');
	    });
	
	    wx.error((err) => {
	      console.error('微信 SDK 初始化失败', err);
	    });
	  } catch (err) {
	    console.error('微信 SDK 初始化失败:', err);
	  }
	},


    loadWxSdk() {
      return new Promise((resolve, reject) => {
        if (window.wx && typeof window.wx.config === 'function') {
          console.log('微信 JS-SDK 已经存在');
          return resolve();
        }
    
        const script = document.createElement('script');
        script.src = 'https://res.wx.qq.com/open/js/jweixin-1.6.0.js';
        script.onload = () => {
          if (window.wx && typeof window.wx.config === 'function') {
            console.log('微信 JS-SDK 脚本加载完成');
            resolve();
          } else {
            reject(new Error('微信 SDK 加载了，但 wx.config 不存在'));
          }
        };
        script.onerror = () => {
          reject(new Error('微信 JS-SDK 脚本加载失败'));
        };
        document.head.appendChild(script);
      });
    },


    onBtnClick1(item) {
		 if(item.title == '视频直播' && item.content){
			 console.log('-----------2---视频直播--------', item.title)
			  uni.navigateTo({
			   url: '/pages/rich/index?title=' + encodeURIComponent(item.title) + '&content=' + encodeURIComponent(item.content)
			 });
			 return
		 }
  //    if(item.title == '视频直播'){
		//  if (window.wx) {
		//       if (item.title == '视频直播') {
		// 		  console.log('点击视频直播--')
		//         wx.miniProgram.navigateTo({
		//           // appId: 'wx87a813bc31d2d99a',
		// 		  appId:'wxf51774d6ef4dda9e',
		// 		  path:'/pages/newindex/index',
		//           // path: '/pages/matchInfo/index?mt_id=35771',
		// 		  envVersion: 'release',  // 如果是正式版本，传 'release'，如果是测试版可传 'trial'

		//           success(res) {
		//             console.log('跳转成功', res);
		//           },
		//           fail(err) {
		//             console.error('跳转失败', err);
		//           }
		//         });
		//       }
		//     } else {
		//       console.error('当前环境不支持跳转到微信小程序');
		//     }
		//  return
	 // }
                     
						window.open(item.link_url);

    },
    onBtnClick2() {
        uni.navigateTo({
          url: '/pages/sszbie/index?group=' + this.groupType
        });
    },
       async fetchNews() {
      let urlstr;
      if (this.groupType == 'AB组') {
        urlstr = 'http://chaoxiang.scoringchina.com/index/api/zhibo_ab';
      } else { 
        urlstr = 'http://chaoxiang.scoringchina.com/index/api/zhibo_cd';
      }
      try {
        const res = await uni.request({
          url: urlstr,
          method: 'GET'
        });
        if(res && res.data && res.data.code === 1) {
         this.groupList = res.data.data || [];
          console.log('-------this.groupList----2-----------', this.groupList)

        } else {
          console.error('获取数据失败', res[1]);
        }
      } catch (error) {
        console.error('请求异常', error);
      }
    },
  }
}
</script>
<style scoped>
.sszbo-container {
  min-height: 100vh;
  background: #f6f8fa;
  padding-bottom: 40rpx;
}
.kv-img {
  width: 100vw;
  display: block;
}
.btn-list {
  margin-top: 24rpx;
  display: flex;
  flex-direction: column;
  gap: 24rpx;
  padding: 0 24rpx;
}
.btn-item {
  min-height: 120rpx;
  border-radius: 20rpx;
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: relative;
  overflow: hidden;
}
.btn-label {
  font-size: 38rpx;
  color: #fff;
  font-weight: bold;
  letter-spacing: 2rpx;
}
.btn-icon {
  width: 704rpx;
  height: 222rpx;
}

</style>
<style>
</style>